/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hva.web.mvcwebapp.doa;

import java.util.ArrayList;
import java.util.List;
import nl.hva.web.mvcwebapp.models.ForumComment;
import nl.hva.web.mvcwebapp.models.ForumTopic;
import nl.hva.web.mvcwebapp.models.ForumThread;
import nl.hva.web.mvcwebapp.models.News;

import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Criteria;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Andrei
 */
public class ForumDAO {

     public ForumDAO() {
    }
    //-----------------------------FORUM TOPIC ---------------------------------------------------------

    public static void newForum(ForumTopic newforum) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.save(newforum);
        tx.commit();
        
    }

    public static int getCourseForum(int course_id) {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(ForumTopic.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction tx = session.beginTransaction();

        Query queryResult = session.createQuery("select forumtopicid from ForumTopic WHERE parentCourse_courseid =" + course_id).setMaxResults(1);
        int topicNumber = Integer.parseInt(queryResult.uniqueResult().toString());
        tx.commit();
        return topicNumber;
    }

    public static List<ForumTopic> getChildTopics(String parenttopic_id) {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(ForumTopic.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        String query = "from ForumTopic WHERE parenttopic_id =" + parenttopic_id;
        Query queryResult = session.createQuery(query);

        List<ForumTopic> topics = queryResult.list();
        tx.commit();
        return topics;
    }

    public static List<ForumTopic> getAllForums() {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(ForumTopic.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        Query queryResult = session.createQuery("from ForumTopic WHERE parenttopic_id = NULL");

        List<ForumTopic> forum_list = queryResult.list();
         tx.commit();
        return forum_list;
    }

    public static ForumTopic getForum(int forum_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumTopic forum = (ForumTopic) session.get(ForumTopic.class, forum_id);
        tx.commit();
        
        return forum;
    }

    public static void insertForum(ForumTopic newForum, int parent_id) {
        ForumTopic parentForum = ForumDAO.getForum(parent_id);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        newForum.setParentTopic(parentForum);
        session.save(newForum);
        tx.commit();
       
    }

    public static void deleteForum(int forum_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumTopic tempForum = (ForumTopic) session.load(ForumTopic.class, forum_id);
        session.delete(tempForum);
        tx.commit();
        
    }
    
     public static void userDeleteTopic(int topic_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumTopic tempTopic =(ForumTopic) session.get(ForumTopic.class, topic_id);
        tempTopic.setForumtopic_isdeleted(1);
        session.update(tempTopic);
        tx.commit();
        
    }

    public static void adminDeleteTopic(int topic_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumTopic tempTopic =(ForumTopic) session.get(ForumTopic.class, topic_id);
        tempTopic.setForumtopic_isdeleted(2);
        session.update(tempTopic);
        tx.commit();
        
    }
    //---------------------------------FORUM THREADS---------------------------------------
    public static void newThread(ForumThread newthread) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.save(newthread);
        tx.commit();
        
    }

    public static List<ForumThread> getAllThreads() {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(ForumThread.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        Query queryResult = session.createQuery("from ForumThread WHERE forumTopic_forumtopicid = NULL");

        List<ForumThread> threads = queryResult.list();
        tx.commit();
        return threads;
    }

    public static ForumThread getThread(int thread_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumThread thread = (ForumThread) session.get(ForumThread.class, thread_id);
        tx.commit();
        
        return thread;
    }

    public static List<ForumThread> getChildThreads(String topic_id) {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(ForumThread.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction tx =session.beginTransaction();
        String query = "from ForumThread WHERE forumTopic_forumtopicid =" + topic_id;
        Query queryResult = session.createQuery(query);

        List<ForumThread> threads = queryResult.list();
        tx.commit();
        return threads;

    }

    public static void insertThread(ForumThread thread, int parenttopic_id) {
        ForumTopic parent = ForumDAO.getForum(parenttopic_id);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        thread.setParentForumTopic(parent);
        session.save(thread);
        tx.commit();
     
 }
    
     public static void userDeleteThread(int thread_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumThread tempThread =(ForumThread) session.get(ForumThread.class, thread_id);
        tempThread.setForumthread_isdeleted(1);
        session.update(tempThread);
        tx.commit();
        
    }

    public static void adminDeleteThread(int thread_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumThread tempThread =(ForumThread) session.get(ForumThread.class, thread_id);
        tempThread.setForumthread_isdeleted(2);
        session.update(tempThread);
        tx.commit();
        
    }


    //-----------------------FORUM COMMENTS =------------------------------------
    public static List<ForumComment> getComments(int thread_id) {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(ForumComment.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        String query = "from ForumComment WHERE forumThread_forumthreadid =" + thread_id;
        Query queryResult = session.createQuery(query);

        List<ForumComment> comments = queryResult.list();
        tx.commit();
        return comments;

    }
    
    public static ForumComment getComment(int comment_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumComment comment = (ForumComment) session.get(ForumComment.class, comment_id);
        tx.commit();
        
        return comment;
    }
    
    
    public static void updateComment(int comment_id, String commentBody) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumComment comment = (ForumComment) session.get(ForumComment.class, comment_id);
        comment.setForumcomment_body(commentBody);
        session.update(comment);
        tx.commit();
       
    }


    public static void insertComment(ForumComment newComment, int thread_id) {
        ForumThread parent = ForumDAO.getThread(thread_id);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        newComment.setForumThread(parent);
        session.save(newComment);
        tx.commit();
        
    }
    
    public static void userDeleteComment(int comment_id) {
        System.out.println("USER DELETES COMMENT, comment_id: " + comment_id);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumComment tempComment =(ForumComment) session.get(ForumComment.class, comment_id);
        tempComment.setForumcomment_isdeleted(1);
        session.update(tempComment);
        tx.commit();
        
    }
    
    public static void adminDeleteComment(int comment_id) {
        System.out.println("DELETE COMMENT ADMIN, comment_id: " + comment_id);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ForumComment tempComment =(ForumComment) session.get(ForumComment.class, comment_id);
        tempComment.setForumcomment_isdeleted(2);
        session.update(tempComment);
        tx.commit();
        
    }
}
